package com.jm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jm.common.util.entity.R;
import com.jm.pojo.Orders;
import com.jm.pojo.dto.OrdersDto;
import com.jm.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单的分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<IPage<OrdersDto>> selectByPage(Long page, Long pageSize, String number, String beginTime, String endTime) {
        LocalDateTime begin = null;
        LocalDateTime end = null;
        if (beginTime != null && endTime != null) {
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            begin = LocalDateTime.parse(beginTime, format);
            end = LocalDateTime.parse(endTime, format);
        }
        IPage<OrdersDto> ordersIPage = orderService.selectByCondition(page, pageSize, number, begin, end);
        return R.success(ordersIPage);
    }

    @GetMapping("/userPage")
    public R<IPage<OrdersDto>> selectByUserPage(Long page, Long pageSize, String number, String beginTime, String endTime) {
        LocalDateTime begin = null;
        LocalDateTime end = null;
        if (beginTime != null && endTime != null) {
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            begin = LocalDateTime.parse(beginTime, format);
            end = LocalDateTime.parse(endTime, format);
        }
        IPage<OrdersDto> ordersIPage = orderService.selectByCondition(page, pageSize, number, begin, end);
        return R.success(ordersIPage);
    }

    /**
     * 提交订单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        orderService.submit(orders);
        return R.success("下单成功");
    }

    //    @PutMapping("/status")
    @PutMapping()
    public R<String> senOrder(@RequestBody Orders orders) {
        boolean status = orderService.status(orders);
        if (!status) return R.error("修改信息失败");
        return R.success("修改信息成功");
    }

    @PostMapping("/again")
    public R<Object> again(@RequestBody Orders orders) {
        Orders order = orderService.getById(orders.getId());
        if (order == null) return R.error("未查询到订单信息");
        return R.success(order);
    }

    @PutMapping("/status")
    public R<Object> uptade(@RequestBody Orders orders) {
        boolean b = orderService.updateById(orders);
        if (!b) R.error("修改失败");
        return R.success("修改成功");
    }

}
